var domain = "354,352,344,341,337,325,248,247,242,241,240,239,238,237,234,232,224,208,207,205,204,203,201,199,198,197,196,195,193,192,191,190,188,187,186,160,139,131,128,124,121,117,115,114,113,108,102,84,83,74,73,70,66,63,61,58,46,45,43,41,40,39,27,26,25,22";
var domain_quaker = "23, 59, 64, 116, 141, 174, 243, 268, 270";
var domain_lays = "24, 60, 65";
var domain_sevenup = "47, 62, 67, 107, 118, 135, 244, 264, 266, 267, 269";
var domain_mirinda = "294, 298, 314, 316, 321, 323";
var domain_gatorade = "49, 69, 72, 271, 272, 273, 274, 275, 276, 277, 278, 279, 282, 283, 286, 307";

var domain = domain + ", " + domain_quaker + ", " + 
	domain_lays + ", " + domain_sevenup + ", " + domain_mirinda + ", " + domain_gatorade;

var brands = [
{"name":"百事可乐", "src":"img/pepsi.gif",
	"datasource":{
		"totalcount": "topic.action?instruction=countCommentNumber&viewName=successs&cache=true&domain="+domain,
		"weibofansGroup" : "topic.action?instruction=GroupOfficialBlog&viewName=keywordtimeline&name="+encodeURIComponent("'百事（中国）有限公司官方微博', '百事饮料8090青年榜样活动官方微博', '昆明百事可乐官方微博', '济南百事可乐饮料有限公司百事可乐品牌官方微博', '百事美食天堂官方微博'")+"&cache=true&showLegend=false",
		"sentimenttrend":"topic.action?instruction=countCommentTimelineWithSentiment&viewName=sentimenttrend_solr&cache=true&domain=" + domain/*"data/sentimenttrend.json"*/,
		"markettrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword=%E4%B8%BA%E6%B8%B4%E6%9C%9B%E5%87%BA%E5%BE%81%2C%E7%99%BE%E4%BA%8B%E6%B7%98%E5%AE%9D%2C%E4%B8%BA%E6%B8%B4%E6%9C%9B%E8%80%8C%E5%88%9B%2C%E7%99%BE%E4%BA%8B%20MJ",// + "&domain=" + domain/*"data/multidata.json"*/,
		"competitortrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword=" + encodeURIComponent('百事可乐,可口可乐'),// + "&domain=" + domain/*"data/multidata.json"*/,
		"relatedbrandtrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword=%E8%80%90%E5%85%8B%2C%E8%8B%B9%E6%9E%9C%E5%85%AC%E5%8F%B8%2CConverse",// + "&domain=" + domain/*"data/multidata.json"*/,
		"area": "topic.action?instruction=groupCommentByLocation&viewName=locationList&cache=true&domain="+domain ,
		"sentiment": /*"data/sentiment.json"*/ "topic.action?instruction=groupBySentiment&viewName=entitylistPepsi_solr&cache=true&domain=" + domain,
		"opinionleader":"topic.action?instruction=groupByAuthor&viewName=authorList&cache=true" + "&domain="+domain +"&name=" + encodeURIComponent('百事'),
		"topickeywords": "topic.action?instruction=groupByTopic&viewName=successs&cache=true&pageSize=50" + "&domain="+domain/*"data/topickeywords.json"*/,
		"activities": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=%E4%B8%BA%E6%B8%B4%E6%9C%9B%E5%87%BA%E5%BE%81%2C%E7%99%BE%E4%BA%8B%E6%B7%98%E5%AE%9D%2C%E4%B8%BA%E6%B8%B4%E6%9C%9B%E8%80%8C%E5%88%9B%2C%E7%99%BE%E4%BA%8B%20MJ",//+ "&domain=" + domain,
		"competitor": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=" + encodeURIComponent('百事可乐,可口可乐'),// + "&domain=" + domain/*"data/competitor.json"*/,
		"relevant": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=%E8%80%90%E5%85%8B%2C%E8%8B%B9%E6%9E%9C%E5%85%AC%E5%8F%B8%2CConverse",
		"datasource": "topic.action?instruction=groupByDataSource&viewName=dataSource&cache=true&domain="+domain/*"data/datasource.json"*/,
		"datasourceTrends":"topic.action?instruction=groupByDataSourceTimeline&viewName=keywordtimeline&cache=true&domain="+domain,
		"buzzvolume":"topic.action?instruction=groupByRootSiteCount&viewName=successs&cache=true&domain="+domain,
		"microfilm": "topic.action?instruction=groupVideoInfoByRootsiteAndTime&viewName=successs&domain=" + domain,
		
		"demo1": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=" + encodeURIComponent('耐克, 苹果, Converse'),
		"demo1_trend":"topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword="+ encodeURIComponent('耐克, 苹果, Converse'),
		
		"demo2": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=" + encodeURIComponent('百事可乐, 可口可乐'),
		"demo2_trend" : "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword="  + encodeURIComponent('百事可乐, 可口可乐'),
		
		"demo3": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=" + encodeURIComponent('肯德基, 麦当劳'),
		"demo3_trend" : "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword=" + encodeURIComponent('肯德基, 麦当劳'),
		
		}},
{"name":"桂格", "src":"img/quaker.jpg",
	"datasource": {
		"totalcount": "topic.action?instruction=countCommentNumber&viewName=successs&cache=true&domain="+domain_quaker,
		"weibofansGroup" : "topic.action?instruction=GroupOfficialBlog&viewName=keywordtimeline&name="+encodeURIComponent("'百事食品（中国）有限公司'")+"&cache=true&showLegend=false",
		"sentimenttrend":"topic.action?instruction=countCommentTimelineWithSentiment&viewName=sentimenttrend_solr&cache=true&domain=" + domain_quaker,
		"markettrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword=" + encodeURIComponent("乐享早餐车,优粮早餐全城热送,隔壁家的桂哥桂嫂"),// + "&domain=" + domain_quaker,
		"competitortrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword=" + encodeURIComponent('桂格,西麦,早早麦,雀巢燕麦'),// + "&domain=" + domain_quaker,
		/* "relatedbrandtrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline&keyword=", */
		"area": "topic.action?instruction=groupCommentByLocation&viewName=locationList&cache=true&domain="+domain_quaker ,
		"sentiment": "topic.action?instruction=groupBySentiment&viewName=entitylistPepsi_solr&cache=true&domain=" + domain_quaker,
		"opinionleader":"topic.action?instruction=groupByAuthor&viewName=authorList&cache=true" + "&domain="+domain_quaker +"&name=" + encodeURIComponent('桂格'),
		"topickeywords": "topic.action?instruction=groupByTopic&viewName=successs&cache=true&pageSize=50" + "&domain="+domain_quaker,
		"activities": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=" + encodeURIComponent("乐享早餐车,优粮早餐全城热送,隔壁家的桂哥桂嫂"),// + "&domain=" + domain_quaker,
		"competitor": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=" + encodeURIComponent('桂格,西麦,早早麦,雀巢燕麦'),// + "&domain=" + domain_quaker,
		/* "relevant": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList&keyword=" + "%E8%80%90%E5%85%8B%2C%E8%8B%B9%E6%9E%9C%E5%85%AC%E5%8F%B8%2CConverse", */
		"datasource": "topic.action?instruction=groupByDataSource&viewName=dataSource&cache=true&domain="+domain_quaker,
		"datasourceTrends":"topic.action?instruction=groupByDataSourceTimeline&viewName=keywordtimeline&cache=true&domain="+domain_quaker,
		// "buzzvolume":"topic.action?instruction=groupByRootSiteCount&viewName=successs&cache=true&domain="+domain_quaker
	} }, 
{"name":"乐事","src":"img/lays.png",
	"datasource": {
		"totalcount": "topic.action?instruction=countCommentNumber&viewName=successs&cache=true&domain="+domain_lays,
	/* 	"weibofansGroup" : "", */
		"sentimenttrend":"topic.action?instruction=countCommentTimelineWithSentiment&viewName=sentimenttrend_solr&cache=true&domain=" + domain_lays,
		"markettrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword=" + encodeURIComponent("百事可乐事"),// + "&domain=" + domain_lays,
		"competitortrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword=" + encodeURIComponent('乐事薯片,品客,可比克,上好佳,好丽友 好友趣'),// + "&domain=" + domain_lays,
	/*	"relatedbrandtrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline&keyword=" + "%E8%80%90%E5%85%8B%2C%E8%8B%B9%E6%9E%9C%E5%85%AC%E5%8F%B8%2CConverse", */
		"area": "topic.action?instruction=groupCommentByLocation&viewName=locationList&cache=true&domain="+domain_lays ,
		"sentiment": /*"data/sentiment.json"*/ "topic.action?instruction=groupBySentiment&viewName=entitylistPepsi_solr&cache=true&domain=" + domain_lays,
		"opinionleader":"topic.action?instruction=groupByAuthor&viewName=authorList&cache=true" + "&domain="+domain_lays +"&name=" + encodeURIComponent('乐事薯片'),
		"topickeywords": "topic.action?instruction=groupByTopic&viewName=successs&cache=true&pageSize=50" + "&domain="+domain_lays,
		"activities": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=" + encodeURIComponent("百事可乐事"),// + "&domain=" + domain_lays,
		"competitor": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=" + encodeURIComponent('乐事薯片,品客,可比克,上好佳,好丽友 好友趣'),// + "&domain=" + domain_lays/*"data/competitor.json"*/,
	/*	"relevant": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList&keyword=" + "%E8%80%90%E5%85%8B%2C%E8%8B%B9%E6%9E%9C%E5%85%AC%E5%8F%B8%2CConverse", */
		"datasource": "topic.action?instruction=groupByDataSource&viewName=dataSource&cache=true&domain="+domain_lays,
		"datasourceTrends":"topic.action?instruction=groupByDataSourceTimeline&viewName=keywordtimeline&cache=true&domain="+domain_lays,
		//"buzzvolume":"topic.action?instruction=groupByRootSiteCount&viewName=successs&cache=true&domain="+domain_lays,
		
		
	} }, 
{"name":"七喜", "src":"img/7up.jpg",
	"datasource": {
		"totalcount": "topic.action?instruction=countCommentNumber&viewName=successs&cache=true&domain="+domain_sevenup,
		"totaltrends":"data/totaltrends.json",
		"weibofansGroup" : "topic.action?instruction=GroupOfficialBlog&viewName=keywordtimeline&name="+encodeURIComponent(" '七喜官方微博','7喜南区微博','冰爽7喜重庆7喜 '")+"&cache=true&showLegend=false",
		"sentimenttrend":"topic.action?instruction=countCommentTimelineWithSentiment&viewName=sentimenttrend_solr&cache=true&domain=" + domain_sevenup/*"data/sentimenttrend.json"*/,
		"markettrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword=" + encodeURIComponent('7喜7夕绿唇勇7秀,谁是主角视频大斗秀'),// + "&domain="+domain_sevenup,
		"competitortrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword=" + encodeURIComponent('七喜,雪碧'),// + "&domain="+domain_sevenup/*"data/multidata.json"*/,
	/*	"relatedbrandtrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline&keyword=" , */
		"area": "topic.action?instruction=groupCommentByLocation&viewName=locationList&cache=true&domain="+domain_sevenup ,
		"sentiment": /*"data/sentiment.json"*/ "topic.action?instruction=groupBySentiment&viewName=entitylistPepsi_solr&cache=true&domain=" + domain_sevenup,
		"opinionleader":"topic.action?instruction=groupByAuthor&viewName=authorList&cache=true" + "&domain="+domain_sevenup +"&name=" + encodeURIComponent('七喜'),
		"topickeywords": "topic.action?instruction=groupByTopic&viewName=successs&cache=true&pageSize=50" + "&domain="+domain_sevenup/*"data/topickeywords.json"*/,
		"activities": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=" + encodeURIComponent('7喜7夕绿唇勇7秀,谁是主角视频大斗秀'),// + "&domain="+domain_sevenup,
		"competitor": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=" + encodeURIComponent('七喜,雪碧'),// + "&domain="+domain_sevenup/*"data/competitor.json"*/,
	/* "relevant": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList&keyword=%E8%80%90%E5%85%8B%2C%E8%8B%B9%E6%9E%9C%E5%85%AC%E5%8F%B8%2CConverse", */
		"datasource": "topic.action?instruction=groupByDataSource&viewName=dataSource&cache=true&domain="+domain_sevenup/*"data/datasource.json"*/,
		"datasourceTrends":"topic.action?instruction=groupByDataSourceTimeline&viewName=keywordtimeline&cache=true&domain="+domain_sevenup,
		//"buzzvolume":"topic.action?instruction=groupByRootSiteCount&viewName=successs&cache=true&domain="+domain_sevenup,
		
	} },
	{"name":"美年达", "src":"",
	"datasource":{
		"totalcount": "topic.action?instruction=countCommentNumber&viewName=successs&cache=true&domain="+domain_mirinda,
		"weibofansGroup" : "topic.action?instruction=GroupOfficialBlog&viewName=keywordtimeline&name="+encodeURIComponent("'美年达南区官方微博'")+"&cache=true&showLegend=false",
		"sentimenttrend":"topic.action?instruction=countCommentTimelineWithSentiment&viewName=sentimenttrend_solr&cache=true&domain=" + domain_mirinda,
		"markettrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword=" + encodeURIComponent('开心年代'),// + "&domain=" + domain_mirinda,
		"competitortrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword=" + encodeURIComponent('佳得乐,芬达,醒目'),// + "&domain=" + domain_mirinda,
		/* "relatedbrandtrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline&keyword="*/
		"area": "topic.action?instruction=groupCommentByLocation&viewName=locationList&cache=true&domain="+domain_mirinda ,
		"sentiment": /*"data/sentiment.json"*/ "topic.action?instruction=groupBySentiment&viewName=entitylistPepsi_solr&cache=true&domain=" + domain_mirinda,
		"opinionleader":"topic.action?instruction=groupByAuthor&viewName=authorList&cache=true" + "&domain="+domain_mirinda +"&name=" + encodeURIComponent('美年达'),
		"topickeywords": "topic.action?instruction=groupByTopic&viewName=successs&cache=true&pageSize=50" + "&domain="+domain_mirinda,
		"activities": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=" + encodeURIComponent('开心年代'),// + "&domain=" + domain_mirinda,
		"competitor": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=" + encodeURIComponent('佳得乐,芬达,醒目'),// + "&domain=" + domain_mirinda,
		/* "relevant": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList&keyword=%E8%80%90%E5%85%8B%2C%E8%8B%B9%E6%9E%9C%E5%85%AC%E5%8F%B8%2CConverse",*/
		"datasource": "topic.action?instruction=groupByDataSource&viewName=dataSource&cache=true&domain="+domain_mirinda/*"data/datasource.json"*/,
		"datasourceTrends":"topic.action?instruction=groupByDataSourceTimeline&viewName=keywordtimeline&cache=true&domain="+domain_mirinda,
		//"buzzvolume":"topic.action?instruction=groupByRootSiteCount&viewName=successs&cache=true&domain="+domain_mirinda,
		
		}},
	{"name":"佳得乐", "src":"",
		"datasource":{
			"totalcount": "topic.action?instruction=countCommentNumber&viewName=successs&cache=true&domain="+domain_gatorade,
			"weibofansGroup" : "topic.action?instruction=GroupOfficialBlog&viewName=keywordtimeline&name="+encodeURIComponent("'佳得乐南区官方微博','佳得乐-上海'")+"&cache=true&showLegend=false",
			"sentimenttrend":"topic.action?instruction=countCommentTimelineWithSentiment&viewName=sentimenttrend_solr&cache=true&domain=" + domain_gatorade,
			"markettrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword=" + encodeURIComponent('佳得乐 NBA'),// + "&domain=" + domain_gatorade,
			"competitortrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline_solr&keyword=" + encodeURIComponent('佳得乐,脉动,宝矿力'),// + "&domain=" + domain_gatorade,
			/* "relatedbrandtrend": "topic.action?instruction=groupSentimentTimelineByKeyWord&cache=true&viewName=keywordtimeline&keyword="*/
			"area": "topic.action?instruction=groupCommentByLocation&viewName=locationList&cache=true&domain="+domain_gatorade ,
			"sentiment": /*"data/sentiment.json"*/ "topic.action?instruction=groupBySentiment&viewName=entitylistPepsi&cache=true&domain=" + domain_gatorade,
			"opinionleader":"topic.action?instruction=groupByAuthor&viewName=authorList&cache=true" + "&domain="+domain_gatorade +"&name=" + encodeURIComponent('佳得乐'),
			"topickeywords": "topic.action?instruction=groupByTopic&viewName=successs&cache=true&pageSize=50" + "&domain="+domain_gatorade,
			"activities": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=" + encodeURIComponent('佳得乐 NBA'),// + "&domain=" + domain_gatorade,
			"competitor": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList_solr&keyword=" + encodeURIComponent('佳得乐,脉动,宝矿力'),// + "&domain=" + domain_gatorade,
			/* "relevant": "topic.action?instruction=groupSentimentByKeyWord&cache=true&viewName=sentimentEntityList&keyword=%E8%80%90%E5%85%8B%2C%E8%8B%B9%E6%9E%9C%E5%85%AC%E5%8F%B8%2CConverse",*/
			"datasource": "topic.action?instruction=groupByDataSource&viewName=dataSource&cache=true&domain="+domain_gatorade/*"data/datasource.json"*/,
			"datasourceTrends":"topic.action?instruction=groupByDataSourceTimeline&viewName=keywordtimeline&cache=true&domain="+domain_gatorade,
			//"buzzvolume":"topic.action?instruction=groupByRootSiteCount&viewName=successs&cache=true&domain="+domain_gatorade,
			
		}}];


window.onload = function() {
	document.getElementById("oneweekinput").checked=true;
	bindSearchAction();
	panelSwitch("brandPanel");
		
	initTime();
	openBrandAnalyticPanel(0);
	
	langSwitch(0);
	langSelect(0);
};
/* switch between panels*/
var panelSwitch = function( activeId ) {
	$(".mainPanel").css("display", "none");
	$("#"+activeId).css("display", "block");
};

var bindSearchAction = function() {
	var searchInput = document.getElementById("searchinput");
	/*searchInput.onkeypress = function(event) {	
		if( event.keyCode == 13 ) {	
			var searchKeyword = searchInput.value;
			searchWithPepsi(searchKeyword);
		}
	};*/
	
	var submit = document.getElementById("searchaction");
	submit.onclick = function() {
		var searchKeyword = searchInput.value;
		searchWithPepsi(searchKeyword);
	};
};


function backNewsDate( n ) {
	$("#newsStartTimeInput").datepicker("setDate", -n);
	$("#newsEndTimeInput").datepicker("setDate", new Date() );
}
function backNewsMonth(n) {
	var date = new Date();
	var year = date.getFullYear();
	var month = date.getMonth();
	var day = date.getDate();
	if( month <= n ) {
		year = Number(year )-1;
		month = month + 12 - n;
	} else {
		month = month - n;
	}
	$("#newsStartTimeInput").datepicker("setDate", new Date(year, month, day));
	$("#newsEndTimeInput").datepicker("setDate", new Date() );
}

function openTopicPanel() {
	panelSwitch("topicPanel");
	
	$('#hottopicTimeInput').datepicker({
		inline: true,
		dateFormat: "yy-mm-dd",
		onSelect: function() { loadTopic("topiclist"); }
	});
	$('#hottopicTimeInput').datepicker("setDate", new Date() );
	
	loadTopic("topiclist");
}


var loadTopic = function(panel) {
	var xmlHttp = new XMLHttpRequest();
	var container = document.getElementById(panel);
	container.innerHTML = '';
	
	xmlHttp.onreadystatechange = function() {
		if( (xmlHttp.readyState == 4) && (xmlHttp.status==200)) {
			
			container.innerHTML = xmlHttp.responseText;
			$("#"+ panel + "  a").hover(
						function() {
							$(this).css('color', 'rgb(9, 166, 218)');
						},
						function() {
							$(this).css('color', '#000');
						}
					);
			$("#topiclist tr:odd").css('background', '#F0F0F0');
		}
	};
	xmlHttp.open('get', 
			"/EagleEye_Client/topic.action?instruction=getTopicCategoryList&viewName=topicCategoryView" +
			"&timeFrom=" + $("#hottopicTimeInput")[0].value );
	xmlHttp.send();
};


function openBrandAnalyticPanel(brandid) {
	initTime();
	panelSwitch("brandPanel");
	currentBrandDatasource = brands[brandid].datasource;
	updateBrandInfo( brands[brandid].datasource );
	
}
function switchNav(nav) {
	$('.selected').removeClass('selected');
	$(nav).addClass('selected');	
}

var updateBrandInfo = function( datasource ) {
	
	updateTotalCount( datasource.totalcount );
	updateBuzzVolume(datasource.buzzvolume);
	
	updateTopicKeywords(datasource.topickeywords);
	
	updateDemoBrand( "demo_Chart_1", datasource.demo1);
	updateDemoBrandTrend("demo_TrendChart_1", datasource.demo1_trend);
	
	updateDemoBrand( "demo_Chart_2", datasource.demo2);
	updateDemoBrandTrend("demo_TrendChart_2", datasource.demo2_trend);
	
	updateDemoBrand( "demo_Chart_3", datasource.demo3);
	updateDemoBrandTrend("demo_TrendChart_3", datasource.demo3_trend);
	
	updateDatasource(datasource.datasource);	
	updateDatasourceTimeline(datasource.datasourceTrends);
	
	
	updateArea( datasource.area );
	updateOpinionLeader( datasource.opinionleader );
};


function initTime() {
	$("#startTimeInput").datepicker({
		inline: true,
		dateFormat: "yy-mm-dd"
	});
	$("#endTimeInput").datepicker({
		inline: true,
		dateFormat: "yy-mm-dd"
	});
	backDate(7);
}
function backDate( n ) {
	$("#startTimeInput").datepicker("setDate", -n);
	$("#endTimeInput").datepicker("setDate", new Date() );
}
function backMonth(n) {
	var date = new Date();
	var year = date.getFullYear();
	var month = date.getMonth();
	var day = date.getDate();
	if( month <= n ) {
		year = Number(year )-1;
		month = month + 12 - n;
	} else {
		month = month - n;
	}
	$("#startTimeInput").datepicker("setDate", new Date(year, month, day));
	$("#endTimeInput").datepicker("setDate", new Date() );
}

var xiSwfUrlStr = "../weibo/flex/playerProductInstall.swf";

function listPages(item, id, label){			  
	 if(!item) return;	
	 
	 var keyword="";
	 var ids = null;
	 var domain = null;
	 var timeFrom = $("#startTimeInput")[0].value;
	 var timeTo = $('#endTimeInput')[0].value;
	 var brand = getBrand(currentBrandDatasource);
	 
	 if("百事可乐" == brand.name){
		 domain=window.domain;
	 }else if("桂格" == brand.name){
		 domain=domain_quaker ;
	 }else if("乐事" == brand.name){
		 domain=domain_lays;
	 }else if("七喜" == brand.name){
		 domain=domain_sevenup ;
	 }else if("美年达" == brand.name ) {
		 domain = domain_mirinda;
	 }else if("佳得乐" == brand.gatorade ){
		 domain = domain_gatorade;
	 }
	 
	 keyword = "+rootsiteid:(" + domain.replace(/,/g, " OR ") + ")";
	 
	 if("pieChart" == id){
		 if(!!item && !!item.entity && !!item.entity.name){
			if(item.entity.name == '正面'){
				keyword += " +similarity:[2 TO *]";
			}else if(item.entity.name == '负面'){
				keyword += " +similarity:[* TO -2]";
			}else if(item.entity.name == '中性'){
				keyword += " +similarity:{-2 TO 2}";
			}else{
				keyword += " +root_site_category_id:" + item.entity.id;
			}
		 }		
	 }else if(!!id && id.indexOf("ids") != -1){
		 timeFrom = item.entity.name;
		 timeTo = item.entity.name;
		 if(!!label && label != '总数'){
			 if(label == '新浪'){
				 keyword += " +root_site_category_id:" + 24;
			 }else if(label == '腾讯'){
				 keyword += " +root_site_category_id:" + 25;
			 }else if(label == '土豆'){
				 keyword += " +root_site_category_id:" + 26;
			 }else if(label == '百度贴吧'){
				 keyword += " +root_site_category_id:" + 27;
			 }else{
				 //keyword += " +" + label;
				 keyword = " +" + label;
			 }
		 }
	 }else if("idPositive" == id){
		 //keyword += " +similarity:[2 TO *]";
		 keyword = " +similarity:[2 TO *]";
		 keyword += " +" + item.entity.name;
	 }else if("idNeutral" == id){
		 //keyword += " +similarity:{-2 TO 2}";
		 keyword = " +similarity:{-2 TO 2}";
		 keyword += " +" + item.entity.name;
	 }else if("idNegative" == id){
		 //keyword += " +similarity:[* TO -2]";
		 keyword = " +similarity:[* TO -2]";
		 keyword += " +" + item.entity.name;
	 }else if("barChart" == id){
		 if(item.entity.type == 'groupByAuthor'){
			 keyword += " +author:" + item.entity.name;
		 }else if(item.entity.type == 'groupCommentByLocation'){
			 keyword += " +address:" + item.entity.name;
		 }
	 }else if("topic" == id) {
		 keyword += label;
	 }
	 
	 //console.log(domain + " " + $("#startTimeInput")[0].value + " " +  $('#endTimeInput')[0].value);	  
	 //alert( keyword );
	 
	 $("input#keyWord")[0].value = keyword;
	 $("input#timeFrom")[0].value = timeFrom;
	 $("input#timeTo")[0].value =  timeTo;
	 $("#webpageIds")[0].value = !ids?"":ids;	
	
	 document.getElementById("fm").submit();
}   


function getBrand(datasource){	
	for(var i=0; i<brands.length; i++){
		if(brands[i].datasource == datasource){
			return brands[i];
		}
	}	
}


function btnHighlight(id) {
}

var langSwitch = ( function() {
	// default language: Chinese
	var langs_type = 0;
	
	var langs = {
			beverage_news:     ['饮料行业', 	'Beverage News'],
			food_news: 		   ['食品行业', 	'Foods News'],
			b_competitor_news: ['竞争对手(饮料行业)', 'Competitors Watch (Beverage)'],
			f_competitor_news: ['竞争对手(食品行业)', 'Competitors Watch (Foods)'],
			pepsi_news: 	   ['百事新闻', 	'PepsiCo News'],
			sevenup_news:      ['七喜新闻',    'SevenUp News'],
			quaker_news:       ['桂格新闻',    'Quaker News'],
			lays_news:         ['乐事新闻',    "Lay's News"],
			mirinda_news:      ['美年达新闻',   'Mirinda News'],
			gatorade_news:     ['佳得乐新闻',   'Garorade News'],
			more_link:         ['更多 ...',    'More...'],
			lastweek: 		   ['过去一周', 	'last week'],
			last2week: 		   ['过去两周', 	'last two weeks'],
			last3week:		   ['过去三周', 	'last three weeks'],
			lastmonth: 		   ['过去一个月',	'last month'],
			last2month: 	   ['过去两个月',	'last two months'],
			last3month:  	   ['过去三个月',	'last three months'],
			updatetime:  	   ['更新时间',    'update'],
			topictitle: 	   ['热点话题',    'Hot Topic'],
			topic_title:       ['标题', 'Title'],
			topic_content:     ['内容', 'Content'],
			topic_days:        ['上榜天数', 'Duration'],
			topic_trend:       ['趋势', 'Trend'],
			topic_sentiment:   ['情感', 'Sentiment'],
			topicreason:       ['热点话题排行根据话题类别，转发及回复数量，作者知名度和上榜时间计算得出', 
			                    'Topic ranking is based on the topic category, replies, author popularity and the duration.'],
			weibofans: 		   ['官方微博粉丝数','Weibo Fans'],
			chart_topic: 	   ['热点话题',    'Hot Topic'],
			chart_buzzvolume:  ['数据总量',    'Buzz Volume'],
			chart_sentiment:   ['总体舆情',    'Sentiment'],
			chart_market:      ['市场活动舆情', 'Sentiment of Marketing Activites'],
			chart_competitor:  ['竞争品牌舆情', 'Sentiment of Competitors'],
			chart_relevantbrand:['相关品牌舆情', 'Sentiment of Relevant Brands'],
			chart_datasource:  ['数据源', 'Data Sources'],
			chart_area: 	   ['地域分布', 'Areas'],
			chart_opinionleader:['意见领袖', 'Opinion Leader'],
			chart_video:        ['微电影', 'Micro Film'],
			chart_brand_demo: ['品牌对比', 'Brands Analysis']
	};
	var traverse = function(node, type) {
		if( node ) {
			if( node.hasAttribute('customlang') ) {
				//console.log(node);
				
				var id =  node.getAttribute('customlang');
				if( id )
					node.innerHTML = langs[id][type];
			}

			$(node).children().each( function(index, element){
				//console.log( element );
				
				traverse(element, type);
			});
		}
	};
	return function(i, elem) {
		if( i != null  )
			langs_type = i;
		if( elem )
			traverse(elem, langs_type );
		else
			traverse(document.body, langs_type );
	};
})();

var langSelect= function(i){
	$('.language a').each( function(index, elem) {
		if( index == i)
			$(this).addClass('language_selected');
		else
			$(this).removeClass('language_selected');
	});
};